var app = getApp();
var timer,info,giftid;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    number:1,
    integral:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    info={};
    var that=this;
    giftid=options.giftid;
    //用户信息
    wx.request({
      url: app.getUrl() + '/api/customer/getCustomer',
      data:{
        customerId:app.globalData.customerId,
      },
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      success(res) {
        if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
          that.setData({
            integral:res.data.obj.totalIntegral
          }) 
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        console.log(res)
        wx.showToast({
          title: '连接服务器失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  reduceNumber(){
    if(this.data.number>=1){
      this.setData({
        number:this.data.number-1
      })
    }
  },
  addNumber(){
    if(this.data.number<this.data.info.limitNum || this.data.info.limitNum=='不限'){
      this.setData({
        number:this.data.number+1
      })
    }else{
      wx.showToast({
        title: '超出限购数量',
        icon:'none'
      })
    }
  },
  inputChange(e){
    if(e.detail.value<this.data.info.limitNum || this.data.info.limitNum=='不限'){
      this.setData({
        number:e.detail.value
      })
    }else{
      wx.showToast({
        title: '超出限购数量',
        icon:'none'
      })
      this.setData({
        number:1
      })
    }
  },
  setTimer(){
    var that=this;
    var hour,minute,second;
    var isOpen=false;
    if(that.data.info.goodsSubscribeInfo && that.data.info.goodsSubscribeInfo.goodsSubscribTime){
      hour = that.data.info.goodsSubscribeInfo.goodsSubscribTime.split(":")[0];
      minute = that.data.info.goodsSubscribeInfo.goodsSubscribTime.split(":")[1];
      second = that.data.info.goodsSubscribeInfo.goodsSubscribTime.split(":")[2];
      isOpen=true;
    }
    timer=setInterval(function(){
      if(isOpen){
        if(second>0){
          second=second-1;  
          info.goodsSubscribeInfo.goodsSubscribTime=hour+'时'+minute+'分'+second+'秒';
          that.setData({
            info:info
          })
        }else{
          if(minute>0){
            minute=minute-1;
            second=59;
            info.goodsSubscribeInfo.goodsSubscribTime=hour+'时'+minute+'分'+second+'秒';
            that.setData({
              info:info
            })
          }else{
            if(hour>0){
              hour=hour-1;
              minute=59;
              second=59;
              info.goodsSubscribeInfo.goodsSubscribTime=hour+'时'+minute+'分'+second+'秒';
              that.setData({
                info:info
              })
            }else{
              info.goodsSubscribeInfo.goodsSubscribTime=hour+'时'+minute+'分'+second+'秒';
              that.setData({
                info:info
              })
              clearInterval(timer)
              that.getInfo()
            }
          }
        }
      }
    },1000)
  },
  getInfo(){
    var that=this;
    //商品详情
    wx.request({
      url: app.getUrl() + '/api/mall/goods_detail',
      data:{
        giftId:giftid,
        Authorization: app.globalData.customerId + ":" + app.globalData.accessToken,
      },
      success(res) {
        if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
          info=res.data.obj;
          if(res.data.obj.limitNum==0){
            res.data.obj.limitNum='不限'
          }
          if(res.data.obj.goodsSubscribeInfo){
            res.data.obj.goodsSubscribeInfo.goodsSubscribeStarttime=res.data.obj.goodsSubscribeInfo.goodsSubscribeStarttime.split("-")[1]+'月'+res.data.obj.goodsSubscribeInfo.goodsSubscribeStarttime.split("-")[2].split(" ")[0]+'日'+res.data.obj.goodsSubscribeInfo.goodsSubscribeStarttime.split("-")[2].split(" ")[1].split(":")[0]+'时'+res.data.obj.goodsSubscribeInfo.goodsSubscribeStarttime.split("-")[2].split(" ")[1].split(":")[1]+'分';
            
            res.data.obj.goodsSubscribeInfo.goodsSubscribeEndtime=+res.data.obj.goodsSubscribeInfo.goodsSubscribeEndtime.split("-")[1]+'月'+res.data.obj.goodsSubscribeInfo.goodsSubscribeEndtime.split("-")[2].split(" ")[0]+'日'+res.data.obj.goodsSubscribeInfo.goodsSubscribeEndtime.split("-")[2].split(" ")[1].split(":")[0]+'时'+res.data.obj.goodsSubscribeInfo.goodsSubscribeEndtime.split("-")[2].split(" ")[1].split(":")[1]+'分';
          }
          that.setData({
            info:info
          })
          if(res.data.obj.giftDetail){
            that.setData({
              nodes: res.data.obj.giftDetail.replace(/\<img/g, "<img style='max-width:100%;height:auto;display:block;border-radius: 10px;'  lazy-load='true'")
            })
          }
          that.setTimer();
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        console.log(res)
        wx.showToast({
          title: '连接服务器失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  //立即预约
  subscribe(){
    var that=this;
    wx.showModal({
      title: '提示',
      content: '确定预约此商品？',
      success (res) {
        if (res.confirm) {
          wx.request({
            url: app.getUrl() + '/api/order_info/subscribe_goods',
            data:{
              goodsId:giftid,
              Authorization: app.globalData.customerId + ":" + app.globalData.accessToken,
            },
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded",
            },
            success(res) {
              if (res && res.statusCode == 200 && res.data && res.data.status == 200) {
                wx.showToast({
                  title: '您已预约成功，请到时抢购！',
                  icon: 'none',
                  duration: 3000
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 3000
                })
              }
            },
            fail(res) {
              console.log(res)
              wx.showToast({
                title: '连接服务器失败',
                icon: 'none',
                duration: 2000
              })
            }
          })          
        } else {}
      }
    })
  },
  sureBuy(){
      //正在热销商品
      if(this.data.info.goodsSubscribeInfo && this.data.info.goodsSubscribeInfo.goodsSubscribStatus=='正在热销' && this.data.info.goodsSubscribeInfo.goodsSubscribMoney==0){
        if(this.data.integral<this.data.number * this.data.info.goodsSubscribeInfo.goodsSubscribIntegral){
          wx.showToast({
            title: '您的积分不足',
            icon:"none"
          })
        }else{
          wx.navigateTo({
            url: '../sureOrder/sureOrder?number='+this.data.number+'&goodsId='+giftid,
          })
        }
      }else if(this.data.info.goodsMoney==0){
        if(this.data.integral<this.data.number * this.data.info.integral){
          wx.showToast({
            title: '您的积分不足',
            icon:"none"
          })
        }else{
          wx.navigateTo({
            url: '../sureOrder/sureOrder?number='+this.data.number+'&goodsId='+giftid,
          })
        }
      }else{
        wx.navigateTo({
          url: '../sureOrder/sureOrder?number='+this.data.number+'&goodsId='+giftid,
        })
      }
  }
})